package com.xiaoyu.lanling.widget.momentgrid

import `in`.srain.cube.views.list.ViewHolderBase
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.media.image.ImageViewUtil.loadImage
import com.xiaoyu.lanling.router.Router

class MomentGridViewHolder : ViewHolderBase<MomentGridItem>() {

    private val sOnClickListener = label@ View.OnClickListener { v: View ->
        val itemData: MomentGridItem = v.getViewTag() ?: return@OnClickListener
        val context: Context = App.getInstance().topActivity ?: return@OnClickListener
        Router.instance.gotoMomentPhotoActivity(context, itemData.imageUrl, itemData.imageUrlList, itemData.feedId)
    }

    private var mSimpleDraweeView: SimpleDraweeView? = null
    private var layoutParams: FrameLayout.LayoutParams? = null
    private val margin: Int = `in`.srain.cube.util.LocalDisplay.dp2px(1f)

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_moment_grid_image, parent, false)
        mSimpleDraweeView = view.findViewById(R.id.simple_drawee_view)
        return view
    }

    override fun showData(position: Int, itemData: MomentGridItem) {
        layoutParams = FrameLayout.LayoutParams(itemData.itemWidth, itemData.itemHeight)
        layoutParams?.setMargins(margin, margin, margin, margin)
        mSimpleDraweeView?.layoutParams = layoutParams
        loadImage(mSimpleDraweeView, itemData.mImageLoadParam)
        mSimpleDraweeView?.setOnClickListener(sOnClickListener)
        mSimpleDraweeView.setViewTag(itemData)
    }
}